//
//  PeopleNearViewController.m
//  HotWord
//
//  Created by Ji Qi on 12-6-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "QAWallDetailViewController.h"
#import "WallDetailAnswerCell.h"
#import "ITTImageView.h"

@interface QAWallDetailViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
{
    BOOL _isFirstLoad;
    Question *_question;
}

@property (retain, nonatomic) IBOutlet UITableView *wallTableView;
@property (retain, nonatomic) IBOutlet UILabel *nullDataLabel;
@property (retain, nonatomic) IBOutlet UIButton *backBtn;
@property (retain, nonatomic) IBOutlet ITTImageView *userHeaderImageView;
@property (retain, nonatomic) IBOutlet UIView *questionContainerView;
@property (retain, nonatomic) IBOutlet UILabel *nickNameLabel;
@property (retain, nonatomic) IBOutlet UILabel *titleLabel;
@property (retain, nonatomic) IBOutlet UILabel *questionLabel;
@property (retain, nonatomic) IBOutlet UILabel *answerCountLabel;
@property (retain, nonatomic) IBOutlet UILabel *answerTagLabel;
@property (retain, nonatomic) IBOutlet UIImageView *fadeImageView;
@property (retain, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation QAWallDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (id)initwithQuestion:(Question *)question
{
    self = [super init];
    if (self)
    {
        _question = [question retain];
    }
    return self;
}

- (void)dealloc {
    [_wallTableView release];
    [_nullDataLabel release];
    [_backBtn release];
    [_userHeaderImageView release];
    [_nickNameLabel release];
    [_titleLabel release];
    [_questionLabel release];
    [_answerCountLabel release];
    [_answerTagLabel release];
    [_question release];
    [_questionContainerView release];
    [_fadeImageView release];
    [_timeLabel release];
    [super dealloc];
}


- (void)viewDidUnload
{
    [self setWallTableView:nil];
    [self setNullDataLabel:nil];
    [self setBackBtn:nil];
    [self setUserHeaderImageView:nil];
    [self setNickNameLabel:nil];
    [self setTitleLabel:nil];
    [self setQuestionLabel:nil];
    [self setAnswerCountLabel:nil];
    [self setAnswerTagLabel:nil];
    [self setQuestionContainerView:nil];
    [self setFadeImageView:nil];
    [self setTimeLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _wallTableView.top = self.view.height;
//    _backBtn.alpha = 0;
    _isFirstLoad = YES;
    [self setQuestionInfo];
    
}

- (void)setQuestionInfo
{
    [_userHeaderImageView setDefaultImage:[UIImage imageNamed:@"photo_card_small.png"]];
    if ([@"1" isEqualToString:_question.isAnonymous])
    {
        _nickNameLabel.text = @"匿名用户";
    }
    else
    {
        [_userHeaderImageView loadImage: _question.avater];
        _nickNameLabel.text = _question.nickName;
    }
    _titleLabel.text = _question.title;
    NSTimeInterval time = [_question.timestamp doubleValue];
    if (time > 0)
    {
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy年M月d日 HH:mm"];
        NSString *dateString = [dateFormatter stringFromDate:date];
        [dateFormatter release];
        _timeLabel.text = dateString;
        
        
    }
    else
    {
        _timeLabel.text = @"";
    }

    _questionLabel.text = [NSString stringWithFormat:@"问：%@",_question.content];
    _answerCountLabel.text = [NSString stringWithFormat:@"%d回复",[_question.answers count]];
    if (IS_STRING_NOT_EMPTY(_question.tag.name))
    {
        _answerTagLabel.text = [NSString stringWithFormat:@"标签：%@ · %@",_question.examTag, _question.tag.name,nil];
    }
    else
    {
        _answerTagLabel.text = [NSString stringWithFormat:@"标签：%@",_question.examTag];
    }
    
    _titleLabel.height = [_titleLabel.text sizeWithFont:_titleLabel.font constrainedToSize:CGSizeMake(_titleLabel.width, 640) lineBreakMode:UILineBreakModeCharacterWrap].height;

    _questionLabel.top = _titleLabel.bottom + 3;
    _questionLabel.height = [_questionLabel.text sizeWithFont:_questionLabel.font constrainedToSize:CGSizeMake(_questionLabel.width, 640) lineBreakMode:UILineBreakModeCharacterWrap].height;
    _questionContainerView.height = _questionLabel.bottom + 36;
    _wallTableView.tableHeaderView = _questionContainerView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _backBtn.alpha = 0;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (_isFirstLoad)
    {
        [UIView animateWithDuration:0.5 animations:^{
            _wallTableView.top = 0;
        }];
        _isFirstLoad = NO;
    }
    
    [UIView animateWithDuration:0.4 animations:^{
        _backBtn.alpha = 1;
    }];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

#pragma mark - IBAction
- (IBAction)tapOnBackBtn:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIScrollViewDelegate

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    [UIView animateWithDuration:0.4 animations:^{
//        if (scrollView.contentOffset.y > 10)
//        {
//            _fadeImageView.alpha = 1;
//        }
//        else
//        {
//            _fadeImageView.alpha = 0;
//        }
//    }];
//}
//
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    [UIView animateWithDuration:0.6 animations:^{
//        _fadeImageView.alpha = 0;
//    }];
//}
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    
//    if (!decelerate)
//    {
//        [UIView animateWithDuration:0.6 animations:^{
//            _fadeImageView.alpha = 0;
//        }];
//    }
//}
//

#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"WallDetailAnswerCell";
    
    WallDetailAnswerCell *cell = (WallDetailAnswerCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"WallDetailAnswerCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    QuestionAnswer *questionAnswer = [_question.answers objectAtIndex:indexPath.row];
    [cell setDataSourceWithQuestionAnswer:questionAnswer];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QuestionAnswer *questionAnswer = [_question.answers objectAtIndex:indexPath.row];
    NSUInteger introductionLabelHeight = [questionAnswer.introduce sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(250, 640) lineBreakMode:UILineBreakModeCharacterWrap].height;
    
    NSString *answer = [NSString stringWithFormat:@"答：%@",questionAnswer.content];
    NSUInteger answerLabelHeight = [answer sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(247, 640) lineBreakMode:UILineBreakModeCharacterWrap].height;
    NSUInteger introductionLabelTop = 47;
    
    return introductionLabelTop + introductionLabelHeight + 6 +  answerLabelHeight + 15;;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_question.answers count];
}




@end
